﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DTO;
namespace DAO
{
    public class LoaiTaiKhoanDA
    {
        public static LoaiTaiKhoan GetByID(int maID)
        {
            LoaiTaiKhoan loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LoaiTaiKhoan_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maLoaiTK", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new LoaiTaiKhoan();
                loai.MaLoaiTK = maID;
                loai.TenTK = Convert.ToString(dataReader["TenTK"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                loai.MaLoaiKyHan = Convert.ToInt32(dataReader["MaLoaiKyHan"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.TKTheFlg= Convert.ToInt32(dataReader["TKTheFlg"]);
                loai.TKTKFlg = Convert.ToInt32(dataReader["TKTKFlg"]);

                loai._tkKyHan = TKKyHanDA.GetByID(loai.MaLoaiKyHan);
            }
            cn.Close();
            return loai;
        }
        public static LoaiTaiKhoan GetByMaLoaiKyHan(int maLoaiKyHan)
        {
            LoaiTaiKhoan loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LoaiTaiKhoan_maLoaiKyhan", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maLoaiKyHan",maLoaiKyHan ));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new LoaiTaiKhoan();
                loai.MaLoaiKyHan = maLoaiKyHan;
                loai.TenTK = Convert.ToString(dataReader["TenTK"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.TKTheFlg = Convert.ToInt32(dataReader["TKTheFlg"]);
                loai.TKTKFlg = Convert.ToInt32(dataReader["TKTKFlg"]);

                loai.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
                loai._tkKyHan = TKKyHanDA.GetByID(loai.MaLoaiKyHan);
            }
            cn.Close();
            return loai;
        }
        public static List<LoaiTaiKhoan> GetAllTKThe()
        {
            List<LoaiTaiKhoan> dsloai = new List<LoaiTaiKhoan>();
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LoaiTaiKhoan_TKThe", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dataReader = cmd.ExecuteReader();
            LoaiTaiKhoan loai;
            while (dataReader.Read())
            {
                loai = new LoaiTaiKhoan();
                loai.MaLoaiTK = Convert.ToInt32(dataReader["MaLoaiTK"]);
                loai.TenTK = Convert.ToString(dataReader["TenTK"]);
                loai.MoTa = Convert.ToString(dataReader["MoTa"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                loai.MaLoaiKyHan = Convert.ToInt32(dataReader["MaLoaiKyHan"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                loai.TKTheFlg = Convert.ToInt32(dataReader["TKTheFlg"]);
                loai.TKTKFlg = Convert.ToInt32(dataReader["TKTKFlg"]);

                loai._tkKyHan = TKKyHanDA.GetByID(loai.MaLoaiKyHan);
                dsloai.Add(loai);
            }
            cn.Close();
            return dsloai;

        }
    }
}
